/**
 * 
 */
package com.hjx.service.system.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hjx.dao.MenuMapper;
import com.hjx.dao.RoleMapper;
import com.hjx.dao.RoleMenuMapper;
import com.hjx.dao.UserRoleMapper;
import com.hjx.model.Menu;
import com.hjx.model.MenuCriteria;
import com.hjx.model.Page;
import com.hjx.model.Role;
import com.hjx.model.RoleCriteria;
import com.hjx.model.RoleMenu;
import com.hjx.model.RoleMenuCriteria;
import com.hjx.model.UserRole;
import com.hjx.model.UserRoleCriteria;
import com.hjx.service.impl.AbstractGenericService;
import com.hjx.service.system.IRoleService;

/**
 * @author huangjixin 2012-7-26, 下午2:23:25
 */
@Service(value = "roleService")
@RemotingDestination(value = "roleService",channels= {"my-amf","my-secure-amf"})
@Transactional
public class RoleServiceImpl extends AbstractGenericService<Role, Integer>
		implements IRoleService {
	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private UserRoleMapper userRoleDao;
	
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	
	@Autowired
	private MenuMapper menuMapper;
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hjx.service.IGenericService#deleteByPrimaryKey(java.io.Serializable)
	 */
	@Override
	public int deleteByPrimaryKey(Integer id) {
		//-- 先删除子表数据
		UserRoleCriteria userRoleCriteria = new UserRoleCriteria();
		userRoleCriteria.createCriteria().andRoleIdEqualTo(id);
		List<UserRole> list = userRoleDao.selectByExample(userRoleCriteria);
		for (UserRole userRole : list) {
			userRoleDao.deleteByPrimaryKey(userRole.getId());
		}
		
		return roleMapper.deleteByPrimaryKey(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hjx.service.IGenericService#insert(java.io.Serializable)
	 */
	@Override
	public int insert(Role record) {
		super.insert(record);
		return roleMapper.insert(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hjx.service.IGenericService#insertSelective(java.io.Serializable)
	 */
	@Override
	public int insertSelective(Role record) {
		super.insertSelective(record);
		return roleMapper.insertSelective(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hjx.service.IGenericService#selectAll()
	 */
	@Override
	public List<Role> selectAll() {
		return roleMapper.selectByExample(new RoleCriteria());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hjx.service.IGenericService#selectByPrimaryKey(java.io.Serializable)
	 */
	@Override
	public Role selectByPrimaryKey(Integer id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hjx.service.IGenericService#updateByPrimaryKeySelective(java.io.
	 * Serializable)
	 */
	@Override
	public int updateByPrimaryKeySelective(Role record) {
		super.updateByPrimaryKeySelective(record);
		return roleMapper.updateByPrimaryKeySelective(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.hjx.service.IGenericService#updateByPrimaryKey(java.io.Serializable)
	 */
	@Override
	public int updateByPrimaryKey(Role record) {
		super.updateByPrimaryKey(record);
		return roleMapper.updateByPrimaryKey(record);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.hjx.service.IGenericService#selectByPage(com.hjx.model.Page)
	 */
	@Override
	public Page selectByPage(Page page) {
		super.selectByPage(page);
		RoleCriteria roleCriteria = new RoleCriteria();
		roleCriteria.setStart(page.getStart());
		roleCriteria.setLimit(page.getLimit());
		int count = roleMapper.countByExample(new RoleCriteria());
		List<Role> list = roleMapper.selectByExample(roleCriteria);
		page.setCount(count);
		page.setList(list);

		return page;
	}

	@Override
	public List<Menu> getByRoleId(Integer roleId) {
		RoleMenuCriteria roleMenuCriteria = new RoleMenuCriteria();
		roleMenuCriteria.createCriteria().andRoleIdEqualTo(roleId);
		List<RoleMenu> list = roleMenuMapper.selectByExample(roleMenuCriteria);
		if(list.size() == 0){
			return null;
		}
		List<Integer> values = new ArrayList<Integer>();
		for (RoleMenu roleMenu : list) {
			values.add(roleMenu.getMenuId());
		}
		
		MenuCriteria menuCriteria = new MenuCriteria();
		menuCriteria.createCriteria().andIdIn(values);
		return menuMapper.selectByExample(menuCriteria);
	}
}
